package com.ambulance.project.manager.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.ambulance.project.manager.domain.BizClass;
import com.ambulance.project.manager.domain.BizDoctor;
import com.ambulance.project.manager.domain.BizNurse;
import com.ambulance.project.manager.mapper.BizClassMapper;
import com.ambulance.project.manager.mapper.BizDoctorMapper;
import com.ambulance.project.manager.mapper.BizNurseMapper;
import com.ambulance.project.manager.service.IBizClassService;
import com.ambulance.project.manager.utils.MgtCommonUtil;
import com.ambulance.project.manager.utils.MgtUserLoginResult;
import com.ambulance.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 班组管理Service业务层处理
 *
 * @author zr
 * @date 2020-05-11
 */
@Service
public class BizClassServiceImpl implements IBizClassService {
    @Autowired
    private BizClassMapper bizClassMapper;
    @Autowired
    private MgtCommonUtil mgtCommonUtil;
    @Autowired
    private BizDoctorMapper bizDoctorMapper;
    @Autowired
    private BizNurseMapper bizNurseMapper;
    @Autowired
    private BizDoctorServiceImpl bizDoctorService;
    @Autowired
    private BizNurseServiceImpl bizNurseService;

    /**
     * 查询班组管理
     *
     * @param classID 班组管理ID
     * @return 班组管理
     */
    @Override
    public BizClass selectBizClassById(Long classID) {
        return bizClassMapper.selectBizClassById(classID);
    }

    /**
     * 查询班组管理列表
     *
     * @param bizClass 班组管理
     * @return 班组管理
     */
    @Override
    public List<BizClass> selectBizClassList(BizClass bizClass) {
        final MgtUserLoginResult mgtUserLoginResult = mgtCommonUtil.getMgtUserLoginResult();
        bizClass.setUid(new Long(mgtUserLoginResult.getUid()));
        final List<BizClass> bizClasses = bizClassMapper.selectBizClassList(bizClass);

        for (BizClass item : bizClasses) {
            final BizDoctor bizDoctor = bizDoctorService.selectBizDoctorById(item.getDoctorID());
            item.setBizDoctor(bizDoctor);
            final BizNurse bizNurse = bizNurseService.selectBizNurseById(item.getNurseID());
            item.setBizNurse(bizNurse);
        }
        return bizClasses;
    }

    /**
     * 新增班组管理
     *
     * @param bizClass 班组管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBizClass(BizClass bizClass) {
        final MgtUserLoginResult mgtUserLoginResult = mgtCommonUtil.getMgtUserLoginResult();
        bizClass.setUid(new Long(mgtUserLoginResult.getUid()));
        //新增班组同时，需要修改进入班组的人员状态
        Long doctorID = bizClass.getDoctorID();
        Long nurseID = bizClass.getNurseID();
        // 校验日期合法
        checkWeek(bizClass);
        int i = bizClassMapper.insertBizClass(bizClass);
        //更新护士
        changeNurseStatus2Class(nurseID, 1, bizClass.getClassID());
        //更新医生
        changeDoctorStatus2Class(doctorID, 1, bizClass.getClassID());
        return i;
    }

    /**
     * 校验日期合法
     *
     * @param bizClass
     */
    private void checkWeek(BizClass bizClass) {
        if (!CommonUtils.checkNull(bizClass.getWeekID())) {
            DateTime now = DateTime.now();
            Integer dayOfWeek = DateUtil.dayOfWeek(now);
            if (dayOfWeek == 1) {
                dayOfWeek = 7;
            } else {
                dayOfWeek = dayOfWeek - 1;
            }
            final List<String> split = StrUtil.split(bizClass.getWeekID(), ',', -1);
            final boolean contains = split.contains(dayOfWeek.toString());
            if (!contains) {
                bizClass.setStatus(1);
            }else{
                bizClass.setStatus(0);
            }
        }
    }

    private void changeDoctorStatus2Class(Long doctorID, Integer status, Long classID) {
        BizDoctor bizDoctor = new BizDoctor();
        bizDoctor.setDoctorID(doctorID);
        bizDoctor.setClassStatus(status);
        bizDoctor.setClassId(classID);
        bizDoctorMapper.updateBizDoctor(bizDoctor);
    }

    private void changeNurseStatus2Class(Long nurseID, Integer status, Long classID) {
        BizNurse bizNurse = new BizNurse();
        bizNurse.setNurseID(nurseID);
        bizNurse.setClassStatus(status);
        bizNurse.setClassId(classID.intValue());
        bizNurseMapper.updateBizNurse(bizNurse);
    }

    /**
     * 修改班组管理
     *
     * @param bizClass 班组管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBizClass(BizClass bizClass) {
        Long classID = bizClass.getClassID();
        checkWeek(bizClass);
        //修改班组将之前旧班组的人员状态修改
        BizClass aClass = bizClassMapper.selectBizClassById(classID);
        Long old_doctorID = aClass.getDoctorID();
        Long now_doctorID = bizClass.getDoctorID();

        Long old_nurseID = aClass.getNurseID();
        Long nurseID = bizClass.getNurseID();
        if (!now_doctorID.equals(old_doctorID)) {
            changeDoctorStatus2Class(old_doctorID, 0, classID);
            changeDoctorStatus2Class(now_doctorID, 1, classID);
        }
        if (!old_nurseID.equals(nurseID)) {
            changeNurseStatus2Class(old_nurseID, 0, classID);
            changeNurseStatus2Class(nurseID, 1, classID);
        }

        return bizClassMapper.updateBizClass(bizClass);
    }

    /**
     * 批量删除班组管理
     *
     * @param classIDs 需要删除的班组管理ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBizClassByIds(Long[] classIDs) {
        for (Long item : classIDs) {
            final BizClass bizClass = selectBizClassById(item);
            changeDoctorStatus2Class(bizClass.getDoctorID(), 0, 0L);
            changeNurseStatus2Class(bizClass.getNurseID(), 0, 0L);
        }
        return bizClassMapper.deleteBizClassByIds(classIDs);
    }

    /**
     * 删除班组管理信息
     *
     * @param classID 班组管理ID
     * @return 结果
     */
    @Override
    public int deleteBizClassById(Long classID) {
        return bizClassMapper.deleteBizClassById(classID);
    }
}
